package com.zr.task.activity;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.BarUtil;
import com.zh.baselibrary.utils.DisplayUtil;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.utils.TTSUtils;
import com.zr.task.R;
import com.zr.task.TaskApplication;
import com.zr.task.constant.Constant;
import com.zr.task.presenter.LoginPresenter;
import com.zr.task.service.HeartService;
import com.zr.task.utils.DeviceUtil;

import butterknife.BindView;
import butterknife.OnClick;


public class QRCodeActivity extends BaseNoIMActivity<LoginPresenter> {

    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.pb)
    ProgressBar pb;

    Bitmap bitmap = null;
    @Override
    protected int getLayoutId() {
        return R.layout.qr_code_activity;
    }

    @Override
    protected void init() {
        ivQrCode.setVisibility(View.GONE);
        Point mPoint = DisplayUtil.getScreenMetrics(this);
        ivQrCode.setLayoutParams(new RelativeLayout.LayoutParams(mPoint.x, mPoint.y));
        new Thread(new Runnable() {
            @Override
            public void run() {
                bitmap = DeviceUtil.getDeviceInfo(QRCodeActivity.this);
                ivQrCode.post(new Runnable() {
                    @Override
                    public void run() {
                        pb.setVisibility(View.GONE);
                        ivQrCode.setVisibility(View.VISIBLE);
                        ivQrCode.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();
        //监听服务端推送的注册成功消息
        registerReceiver(mBroadcastReceiver, new IntentFilter(BaseConstant.BROAT_CAST_ACTION_FIRST_LOGIN));
        TTSUtils.getInstance().init(TaskApplication.getContext());
    }
    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            startService(new Intent(QRCodeActivity.this, HeartService.class));
            mPresenter.login();
            if (Constant.isBid)
                IntentUtils.instance().startActivityFinish(QRCodeActivity.this, BidActivity.class);
            else
                IntentUtils.instance().startActivityFinish(QRCodeActivity.this, TaskMainActivity.class);
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }

    }
    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }
    @OnClick(R.id.iv_qr_code)
    public void onViewClicked(View view) {
        threeClick();
    }
    int count;

    Long start, end;

    private void threeClick() {
        count++;
        if (count == 1) {
            start = System.currentTimeMillis();
        }
        if (count == 3) {
            end = System.currentTimeMillis();
        }
        if (count >= 3) {
            if ((end - start) < 700) {
                BarUtil.showBar();
                finish();
                Intent intent = new Intent();// 为Intent设置Action、Category属性
                intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
                intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
                startActivity(intent);
            }
            count = 0;
        }
        if ((System.currentTimeMillis() - start) > 1000) {
            count = 0;
            start = System.currentTimeMillis();
        }
    }
}
